package cfca.seal.front.service.servlet;

import cfca.sadk.util.Base64;
import cfca.seal.front.service.FunctionType;
import cfca.seal.front.service.ws.IBusinessSealService;
import cfca.seal.util.StringUtil;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class BusinessSealServlet extends HttpServlet
{
  private static final long serialVersionUID = -2802334706155081655L;
  private static final Logger logger = LoggerFactory.getLogger("service_sys");

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    request.setCharacterEncoding("UTF-8");
    String type = request.getParameter("type");
    OutputStream os = response.getOutputStream();
    try {
      ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
      IBusinessSealService businessSealService = (IBusinessSealService)applicationContext.getBean("businessSealServiceImpl");

      if (type.equals(FunctionType.synthesizeAutoBusinessPdf.toString()))
      {
        String pdfString = request.getParameter("pdf");
        byte[] pdf = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pdfString)) {
          pdf = Base64.decode(pdfString.getBytes("UTF-8"));
        }
        String fieldListXML = request.getParameter("fieldListXML");
        if (StringUtil.isNotEmpty(fieldListXML)) {
          fieldListXML = new String(Base64.decode(fieldListXML.getBytes("UTF-8")), "UTF-8");
        }
        String imageListXML = request.getParameter("imageListXML");
        if (StringUtil.isNotEmpty(imageListXML)) {
          imageListXML = new String(Base64.decode(imageListXML.getBytes("UTF-8")), "UTF-8");
        }
        byte[] synthesizeAutoBusinessPdf = businessSealService.synthesizeAutoBusinessPdf(pdf, fieldListXML, imageListXML);
        byte[] result = Base64.encode(synthesizeAutoBusinessPdf);
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.transformToPdf.toString()))
      {
        String sourceString = request.getParameter("source");
        byte[] source = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(sourceString)) {
          source = Base64.decode(sourceString.getBytes("UTF-8"));
        }
        String fileType = request.getParameter("fileType");
        if (StringUtil.isNotEmpty(fileType)) {
          fileType = new String(Base64.decode(fileType.getBytes("UTF-8")), "UTF-8");
        }
        byte[] transformToPdf = businessSealService.transformToPdf(source, fileType);
        byte[] result = Base64.encode(transformToPdf);
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.autoConcatPdfs.toString()))
      {
        String pdf1String = request.getParameter("pdf1");
        byte[] pdf1 = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pdf1String)) {
          pdf1 = Base64.decode(pdf1String.getBytes("UTF-8"));
        }
        String pdf2String = request.getParameter("pdf2");
        byte[] pdf2 = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pdf2String)) {
          pdf2 = Base64.decode(pdf2String.getBytes("UTF-8"));
        }
        byte[] autoConcatPdfs = businessSealService.autoConcatPdfs(pdf1, pdf2);
        byte[] result = Base64.encode(autoConcatPdfs);
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.autoConcatPdfList.toString()))
      {
        String pdfBase64List = request.getParameter("pdfBase64List");
        if (StringUtil.isNotEmpty(pdfBase64List)) {
          pdfBase64List = new String(Base64.decode(pdfBase64List.getBytes("UTF-8")), "UTF-8");
        }
        String outputFilePath = request.getParameter("outputFilePath");
        if (StringUtil.isNotEmpty(outputFilePath)) {
          outputFilePath = new String(Base64.decode(outputFilePath.getBytes("UTF-8")), "UTF-8");
        }
        String autoConcatPdfs = businessSealService.autoConcatPdfList(pdfBase64List, outputFilePath);
        byte[] result = Base64.encode(autoConcatPdfs.getBytes("UTF-8"));
        os.write(result, 0, result.length);
      } else if (type.equals(FunctionType.addBlankSignature2Pdf.toString()))
      {
        String pdfString = request.getParameter("pdf");
        byte[] pdf = "".getBytes("UTF-8");
        if (StringUtil.isNotEmpty(pdfString)) {
          pdf = Base64.decode(pdfString.getBytes("UTF-8"));
        }
        String blankSignatureStrategyXML = request.getParameter("blankSignatureStrategyXML");
        if (StringUtil.isNotEmpty(blankSignatureStrategyXML)) {
          blankSignatureStrategyXML = new String(Base64.decode(blankSignatureStrategyXML.getBytes("UTF-8")), "UTF-8");
        }

        byte[] addBlankSignature2Pdf = businessSealService.addBlankSignature2Pdf(pdf, blankSignatureStrategyXML);

        byte[] result = Base64.encode(addBlankSignature2Pdf);
        os.write(result, 0, result.length);
      }

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    catch (Exception e)
    {
      logger.error("C0001", e);
      StringBuilder result = new StringBuilder();
      result.append("<Error><errorCode>");
      result.append("C0001");
      result.append("</errorCode><ErrorMessage>");
      result.append("错误");
      result.append("</ErrorMessage>");
      response.getOutputStream().write(Base64.encode(result.toString().getBytes("UTF-8")));

      if (null != os) {
        os.flush();
        os.close();
      }
    }
    finally
    {
      if (null != os) {
        os.flush();
        os.close();
      }
    }
  }
}